package com.systop.asset.controller;

import java.util.List;

import com.github.pagehelper.PageInfo;
import com.systop.asset.entity.RestResponse;
import com.systop.asset.entity.vo.AssetAssignmentVO;
import com.systop.asset.utils.PageUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.systop.asset.entity.AssetAssignments;
import com.systop.asset.service.IAssetAssignmentsService;


/**
 * 资产领用记录Controller
 * 
 * @author mc
 * @date 2025-06-30
 */
@RestController
@RequestMapping("/system/assignments")
public class AssetAssignmentsController
{
    @Autowired
    private IAssetAssignmentsService assetAssignmentsService;
    /**
     * 新增资产领用记录-小程序
     */
    @PostMapping("/addAssetBorrow")
    public RestResponse addAssetBorrowByWechat(@RequestBody AssetAssignments assetAssignments)
    {
        //新增资产领用记录
        return RestResponse.success(assetAssignmentsService.insertAssetAssignmentsByWechat(assetAssignments));

    }
    /**
     * 查询资产领用记录列表
     */
    @GetMapping("/list")
    public RestResponse list(AssetAssignments assetAssignments)
    {
        if (assetAssignments.getUseStatus()!=null && assetAssignments.getUseStatus()==2){
            return RestResponse.success(assetAssignmentsService.selectAssetAssignmentsList(assetAssignments));
        }
        PageUtils.startPage();
        //领用类型 0-资产 1-耗材
        List<AssetAssignments> list = assetAssignmentsService.selectAssetAssignmentsList(assetAssignments);
        return RestResponse.success(new PageInfo<>(list));
    }

    /**
     * 查询耗材领用记录列表
     */
    @GetMapping("/materialList")
    public RestResponse materialList(AssetAssignments assetAssignments)
    {
        PageUtils.startPage();
        //耗材领用列表
        List<AssetAssignments> list = assetAssignmentsService.selectAssetAssignmentsListByMaterial(assetAssignments);
        return RestResponse.success(new PageInfo<>(list));
    }
    /**
     * 查询耗材领用记录列表-小程序
     */
    @GetMapping("/materialListByWechat")
    public RestResponse materialListByWechat(AssetAssignments assetAssignments)
    {
        PageUtils.startPage();
        //耗材领用列表
        List<AssetAssignments> list = assetAssignmentsService.materialListByWechat(assetAssignments);
        return RestResponse.success(new PageInfo<>(list));
    }


    /**
     * 获取资产领用记录详细信息
     */
    @GetMapping
    public RestResponse getInfo(@PathVariable("id") Long id)
    {
        return RestResponse.success(assetAssignmentsService.selectAssetAssignmentsById(id));
    }

    /**
     * 新增资产、资产领用记录
     */
    @PostMapping
    public RestResponse add(@RequestBody AssetAssignments assetAssignments)
    {
        if (assetAssignments.getType()==null || assetAssignments.getType()==0){
            //新增资产领用记录
            return RestResponse.success(assetAssignmentsService.insertAssetAssignments(assetAssignments));
        }else {
            //新增耗材出库领用记录
            return RestResponse.success(assetAssignmentsService.insertAssetAssignmentsByMaterial(assetAssignments));
        }

    }

    /**
     * 修改资产领用记录
     */
    @PutMapping
    public RestResponse edit(@RequestBody AssetAssignments assetAssignments)
    {
        return RestResponse.success(assetAssignmentsService.updateAssetAssignments(assetAssignments));
    }

    /**
     * 删除资产领用记录
     */
    @DeleteMapping("/{ids}")
    public RestResponse remove(@PathVariable Long[] ids)
    {
        return RestResponse.success(assetAssignmentsService.deleteAssetAssignmentsByIds(ids));
    }
    /**
     * 查询资产\耗材领用详情记录列表
     */
    @GetMapping("/detail")
    public RestResponse detail(String id)
    {
        return RestResponse.success(assetAssignmentsService.selectDetail(id));
    }

    /**
     * 查询耗材领用详情记录列表
     * @param id
     * @param type
     * @return
     */
    @GetMapping("/materialDetail")
    public RestResponse materialDetail(String id,Integer type){
        return RestResponse.success(assetAssignmentsService.selectDetailByMaterial(id,type));
    }

    /**
     * 查询资产领用详情记录列表
     */
    @GetMapping("/detailAll")
    public RestResponse detailAll(String id)
    {
        AssetAssignmentVO assetAssignments = assetAssignmentsService.selectDetailAll(id);
        return RestResponse.success(assetAssignments);
    }
}
